package com.banmao.algorithm.No2301To2400.No2363;

import java.util.ArrayList;
import java.util.List;

// 合并相似的物品
public class Solution {

    // 最简单的方法，既然每个二维数组中value是唯一的，那就让它们映射到一个一维数组上
    public List<List<Integer>> mergeSimilarItems(int[][] items1, int[][] items2) {

        int[] weights = new int[1001]; // 下标是value，值为weight

        for (int[] item1 : items1) {
            int value = item1[0];
            int weight = item1[1];

            int temp = weights[value]; // 取出映射数组中以value为下标的值
            weights[value] = temp + weight; // 相加放回去
        }
        for (int[] item2 : items2) {
            int value = item2[0];
            int weight = item2[1];

            int temp = weights[value]; // 取出映射数组中以value为下标的值
            weights[value] = temp + weight; // 相加放回去
        }

        ArrayList<List<Integer>> list2 = new ArrayList<>();
        for (int i = 0; i < weights.length; i++) {
            if(weights[i] > 0) {
                ArrayList<Integer> list = new ArrayList<>();
                list.add(i);
                list.add(weights[i]);
                list2.add(list);
            }
        }

        return list2;
    }

}
